"""
author= '李昊凌'
Create Time = 2020/7/2315:00
Change Time = 20/7/23
commit:"预约访问数据库模块"
"""

import datetime
from time import sleep
from parking_database.appointment_record import *
def query_plate(plate):#查询预约
    isHave,result = query_appointment_by_plate1(plate)
    if isHave==True:
        return "预约发起于："+str(result[2])+" 持续时间："+str(result[3])
    else:
        return "没有查询到预约"

def add_app(plate,hour,min):#新建预约
    time=datetime.timedelta(0,min*60+hour*3600)
    add_appointment(plate,time)
def app_acomplete(plate):#完成预约，在入库时使用
    isHave =  query_appointment_by_plate(plate)
    if isHave:
        complete_appointment(plate)
        print("预约完成")
        return '预约车'
    else:
        print("没有对应预约")
        return ''
def app_overtime():#删除过期预约，在脚本中运行
    apps=query_appointment_all_ddl()
    print(apps)
    for item in apps :
        ddlApp=item["预约期限"]
        print(ddlApp)
        print(type(ddlApp))
        timeNow = datetime.datetime.now()

        if timeNow.__gt__(ddlApp):
            app_acomplete(item["车牌"])
            print("删除过期！")
        else:
            print("正常")


if __name__=="__main__":
    add_app("云Q03538",0,5)
    while(True):
        print("运行")
        app_overtime()
        sleep(15)
